package com.justgame.project.service.art.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.justgame.project.cloud.common.constant.Result;
import com.justgame.project.cloud.common.util.MapBuilder;
import com.justgame.project.service.art.entity.ArtPageInfo;
import com.justgame.project.service.art.dto.ArtPageInfoDTO;
import com.justgame.project.service.art.wrapper.ArtPageInfoWrapper;
import com.justgame.project.service.art.controller.form.ArtPageInfoQueryForm;
import com.justgame.project.service.art.service.IArtPageInfoService;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import io.swagger.annotations.Api;
import lombok.RequiredArgsConstructor;
import org.apache.shiro.authz.annotation.RequiresAuthentication;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.util.Assert;
import javax.validation.constraints.NotNull;
import java.util.List;

/**
 *
 * 文章信息表控制器
 *
 * @@tableName art_page_info
 * @author Zwb
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/art/page/info")
@Api("文章信息表控制器")
public class ArtPageInfoController {

    private final IArtPageInfoService artPageInfoService;

    @GetMapping("/detail")
    @ApiOperation(value = "通过主键id查询 文章信息表")
    public Result<?> query(@ApiParam("主键id") @NotNull(message = "必须提供需要查询的数据") Long id){
        return Result.ok(artPageInfoService.getById(id));
    }

    @PutMapping("/update")
    @RequiresAuthentication
    @ApiOperation(value = "通过主键id编辑 文章信息表")
    public Result<?> edit(@ApiParam("编辑表单") @RequestBody @Validated ArtPageInfoDTO dto){
        Assert.notNull(dto.getId(),"必须提供id");
        Assert.notNull(dto.getImageUrl(),"必须提供封面图片");
        Assert.notNull(dto.getTitle(),"必须提供标题");
        Assert.notNull(dto.getContent(),"必须提供文章内容");
        Assert.notNull(dto.getType(),"必须提供类型");
        Assert.notNull(dto.getWatchCount(),"必须提供观看次数");
        Assert.notNull(dto.getShareCount(),"必须提供分享次数");
        Assert.notNull(dto.getLikeCount(),"必须提供点赞次数");
        Assert.notNull(dto.getIsHidden(),"必须提供是否隐藏");
        ArtPageInfo artPageInfo = artPageInfoService.requireGetById(dto.getId(),"相关数据不存在");
        artPageInfo.setImageUrl(dto.getImageUrl());
        artPageInfo.setTitle(dto.getTitle());
        artPageInfo.setContent(dto.getContent());
        artPageInfo.setType(dto.getType());
        artPageInfo.setWatchCount(dto.getWatchCount());
        artPageInfo.setShareCount(dto.getShareCount());
        artPageInfo.setLikeCount(dto.getLikeCount());
        artPageInfo.setIsHidden(dto.getIsHidden());
        return Result.status(artPageInfoService.updateById(artPageInfo));
    }

    @PostMapping("/save")
    @RequiresAuthentication
    @ApiOperation(value = "新增 文章信息表")
    public Result<?> save(@ApiParam("新增表单") @RequestBody @Validated ArtPageInfoDTO dto){
        Assert.notNull(dto.getImageUrl(),"必须提供封面图片");
        Assert.notNull(dto.getTitle(),"必须提供标题");
        Assert.notNull(dto.getContent(),"必须提供文章内容");
        Assert.notNull(dto.getType(),"必须提供类型");
        Assert.notNull(dto.getWatchCount(),"必须提供观看次数");
        Assert.notNull(dto.getShareCount(),"必须提供分享次数");
        Assert.notNull(dto.getLikeCount(),"必须提供点赞次数");
        Assert.notNull(dto.getIsHidden(),"必须提供是否隐藏");
        ArtPageInfo artPageInfo = new ArtPageInfo();
        artPageInfo.setImageUrl(dto.getImageUrl());
        artPageInfo.setTitle(dto.getTitle());
        artPageInfo.setContent(dto.getContent());
        artPageInfo.setType(dto.getType());
        artPageInfo.setWatchCount(dto.getWatchCount());
        artPageInfo.setShareCount(dto.getShareCount());
        artPageInfo.setLikeCount(dto.getLikeCount());
        artPageInfo.setIsHidden(dto.getIsHidden());
        return Result.status(artPageInfoService.save(artPageInfo));
    }

    @DeleteMapping("/remove")
    @RequiresAuthentication
    @ApiOperation(value = "通过主键id删除 文章信息表")
    public Result<?> remove(@ApiParam("主键id") @NotNull(message = "必须提供需要删除的数据") Long id){
        return Result.status(artPageInfoService.requireRemoveById(id, "抱歉，您的账号权限不支持删除此数据"));
    }

    @GetMapping("/page")
    @ApiOperation(value = "分页查询")
    public Result<?> page(@ApiParam("查询表单") ArtPageInfoQueryForm queryForm){
        IPage<ArtPageInfoWrapper> page = artPageInfoService.selectJoinListPage(queryForm.buildPageQuery());
        return Result.ok(MapBuilder.builder()
                    .put("data",page.getRecords())
                    .put("rows",page.getTotal())
                .build());
    }

    @GetMapping("/list")
    @ApiOperation(value = "列表查询")
    public Result<?> list(@ApiParam("查询表单") ArtPageInfoQueryForm queryForm){
        List<ArtPageInfoWrapper> list = artPageInfoService.selectJoinList(queryForm.buildListQuery());
        return Result.ok(MapBuilder.builder()
                    .put("data",list)
                .build());
    }
}